﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class NewDuctTypeForm : System.Windows.Forms.Form
	{
		public NewDuctTypeForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.initcmbTypeName();
			if (this.cmbParentType.SelectedItem != null)
			{
				this.hytbTypeName.Text = (this.cmbParentType.SelectedItem as ListItem).Name;
			}
		}

		public NewDuctTypeForm(Autodesk.Revit.DB.Document doc, Element elem, ListBox lll)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.lb = lll;
			if (elem is PipeType)
			{
				this.isPipe = true;
				this.pipeType = (elem as PipeType);
				this.hytbTypeName.Text = this.pipeType.Name;
				this.initcmbTypeName();
				this.SelectedItemByName(this.pipeType.Id);
				this.cmbParentType.Enabled = false;
				return;
			}
			if (elem is DuctType)
			{
				this.isPipe = false;
				this.ductType = (elem as DuctType);
				this.hytbTypeName.Text = this.ductType.Name;
				this.initcmbTypeName();
				this.SelectedItemByName(this.ductType.Id);
				this.cmbParentType.Enabled = false;
			}
		}

		private void SelectedItemByName(ElementId name)
		{
			foreach (object obj in this.cmbParentType.Items)
			{
				ListItem listItem = (ListItem)obj;
				if (name == new ElementId(int.Parse(listItem.Value)))
				{
					this.cmbParentType.SelectedItem = listItem;
					break;
				}
			}
		}

		private void initcmbTypeName()
		{
			this.cmbParentType.DisplayMember = "Name";
			this.cmbParentType.ValueMember = "Value";
			if (this.isPipe)
			{
				this.pipeTypeList = YJKElementFilter.GetElementByClass(typeof(PipeType), this.doc);
			}
			else
			{
				this.pipeTypeList = YJKElementFilter.GetElementByClass(typeof(DuctType), this.doc);
			}
			foreach (Element element in this.pipeTypeList)
			{
				ListItem item = new ListItem(element.Name, element.Id.IntegerValue.ToString());
				this.cmbParentType.Items.Add(item);
			}
			if (this.cmbParentType.Items.Count > 0 && this.cmbParentType.Items[0] != null)
			{
				this.cmbParentType.SelectedItem = this.cmbParentType.Items[0];
			}
		}

		public bool JudgeNameContainTeShu(string name)
		{
			bool result = false;
			if (name.Contains("\\") || name.Contains(":") || name.Contains("{") || name.Contains("}") || name.Contains("[") || name.Contains("]") || name.Contains("|") || name.Contains(";") || name.Contains("<") || name.Contains(">") || name.Contains("?") || name.Contains("`") || name.Contains("~"))
			{
				result = true;
			}
			return result;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			string text = this.hytbTypeName.Text.Trim();
			if (string.IsNullOrEmpty(text) || this.JudgeNameContainTeShu(text))
			{
				MessageBox.Show("类型名称不能为空,并且不能包含下列任一字符：\\:{}[]|;<>?`~！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.hytbTypeName.Focus();
				return;
			}
			if (this.IsTypeNameExist(text))
			{
				MessageBox.Show("类型名称已存在", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.hytbTypeName.Focus();
				return;
			}
			if (this.pipeType == null || this.ductType == null)
			{
				ElementId elementId = new ElementId(int.Parse((this.cmbParentType.SelectedItem as ListItem).Value));
				if (this.isPipe)
				{
					this.pipeType = (this.doc.GetElementById(elementId) as PipeType);
				}
				else
				{
					this.ductType = (this.doc.GetElementById(elementId) as DuctType);
				}
			}
			if (this.pipeType != null)
			{
				this.pipeType.Duplicate(text);
			}
			if (this.ductType != null)
			{
				this.ductType.Duplicate(text);
			}
			this.doc.Regenerate();
			this.pipeTypeName = this.hytbTypeName.Text.Trim();
			MessageBox.Show("创建成功", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			base.DialogResult = DialogResult.OK;
		}

		private bool IsTypeNameExist(string name)
		{
			bool result = false;
			IEnumerator enumerator = this.lb.Items.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((ListItem)enumerator.Current).Name == name)
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private void cmbParentType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.hytbTypeName.Text = (this.cmbParentType.SelectedItem as ListItem).Name;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private Document doc;

		private PipeType pipeType;

		private DuctType ductType;

		public string pipeTypeName = "";

		private List<Element> pipeTypeList;

		private bool isPipe;

		private ListBox lb;
	}
}
